using Jupiter.Infrastructure;
using Jupiter.Infrastructure.Cache.Interfaces;
using Jupiter.Infrastructure.Domain;
using Jupiter.Infrastructure.Domain.SqlSugar;
using Jupiter.Infrastructure.Domain.SqlSugar.Basic;
using Jupiter.Infrastructure.Exceptions;
using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.DataPermissions;
using MediatR;
using Microsoft.Extensions.Configuration;
using Wips.AppService.Authorities.Operations.Requests;
using Wips.Domain.Models.Authorities.Model;

namespace Wips.AppService.SqlSugar.Authorities.Operations
{
    /// <summary>
    /// 操作请求处理程序
    /// </summary>
    public class OperationRequestHandler :
        RequestHandlerBase<Operation>,
        IRequestHandler<CreateOperationRequest, string>,
        IRequestHandler<UpdateOperationRequest, string>,
        IRequestHandler<DeleteOperationRequest, string>
    {
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> Handle(CreateOperationRequest request,
            CancellationToken cancellationToken)
        {
           
            // 封装实体对象
            var entity = new Operation();
            entity.Id = ObjectId.GenerateNewStringId();
            entity.CreatorId = UserId!;
            entity.LastUpdatedUserId = UserId!;
            entity.Code = request.Code;
            entity.MenuId = request.MenuId;
            entity.Name = request.Name;
            entity.Sort = request.Sort;
            await RegisterNewAsync(entity, cancellationToken);

            return entity.Id;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<string> Handle(UpdateOperationRequest request,
            CancellationToken cancellationToken)
        {
            // 封装实体对象
            var entity = await Query()
                .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);

            if (entity == null)
            {
                throw new CoreException("找不到记录");
            }

            entity.Name = request.Name;
            entity.Code = request.Code;
            entity.Sort = request.Sort;
            entity.LastUpdatedUserId = UserId;

            // 更新
            RegisterDirty(entity);
            // 提交

            return entity.Id!;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        public async Task<string> Handle(DeleteOperationRequest request,
            CancellationToken cancellationToken)
        {
            // 读取数据
            var entity = await Query()
                .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
            if (entity == null)
            {
                throw new CoreException("找不到要删除的数据");
            }

            // 删除
            RegisterDelete(entity);
            return "";
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="securityContextAccessor"></param>
        /// <param name="facadeResolver"></param>
        /// <param name="cacheManager"></param>
        /// <param name="configuration"></param>
        /// <param name="dataPermission"></param>
        public OperationRequestHandler(ISqlSugarClient dbContext, ISecurityContextAccessor securityContextAccessor,
            IFacadeResolver facadeResolver, ICacheManager cacheManager, IConfiguration configuration,
            IDataPermission dataPermission) : base(
            dbContext, securityContextAccessor, facadeResolver, cacheManager, configuration, dataPermission)
        {
        }
    }
}